package com.fhz.main

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.appcompat.app.AppCompatActivity
import com.fhz.main.databinding.ActivityWelcomeBinding
import java.lang.ref.WeakReference


class WelcomeActivity : AppCompatActivity() {

    private val weakHandler by lazy { WeakReferenceHandler(this) }

    class WeakReferenceHandler(obj: WelcomeActivity) : Handler(Looper.getMainLooper()) {
        private val mRef: WeakReference<WelcomeActivity> = WeakReference(obj)

        override fun handleMessage(msg: Message) {
            mRef.get()?.run {
                when (msg.what) {
                    1 -> {
                        //启动主界面
                        MainActivity.lunch(this)
                    }
                    else -> {}
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityWelcomeBinding.inflate(layoutInflater)
        setContentView(binding.root)


       val objectAnimator =  ObjectAnimator.ofFloat(binding.imWelcomeCircle,"rotation",
           (-90).toFloat(),0.toFloat())
        binding.imWelcomeCircle.pivotX = 0.toFloat()
        binding.imWelcomeCircle.pivotY = binding.imWelcomeCircle.height.toFloat()
        objectAnimator.duration = 2500
        objectAnimator.start()

        val tran = ObjectAnimator.ofFloat(
            binding.imWelcomeStart,
            "translationX",
            (-200).toFloat(),
            0.toFloat()
        )
        tran.duration = 2500
        tran.start()

        val jian = ObjectAnimator.ofFloat(
            binding.imWelcomeStartJian,
            "translationX",
            (-200).toFloat(),
            0.toFloat()
        )
        jian.duration = 2500
        jian.start()

        val tx_scaleX = ObjectAnimator.ofFloat(binding.tvWelcome, "scaleX",0.toFloat(),1.toFloat())
        val tx_scaleY = ObjectAnimator.ofFloat(binding.tvWelcome, "scaleY",0.toFloat(),1.toFloat())
        val animatorSet = AnimatorSet()
        animatorSet.duration = 2500
        animatorSet.playTogether(tx_scaleY,tx_scaleX)
        animatorSet.start()



        weakHandler.sendEmptyMessageDelayed(1,1)

    }

    override fun onDestroy() {
        super.onDestroy()
        weakHandler.removeCallbacksAndMessages(null)
    }
}